package net.zoneland.knowledge.enums;

import net.zoneland.knowledge.model.WorkApplyInfo;
import net.zoneland.knowledge.model.WorkApproveOpinion;
import net.zoneland.knowledge.utils.DateHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.poi.ss.usermodel.Sheet;

import java.util.Comparator;
import java.util.Optional;

/**
 * 用章审批打印的审批表，字段映射枚举.
 * @author bcc
 */
public enum ChapterApproveFormExcelEnum {
    CREATOR_PARTY_ORG_NAME(1,1) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return workApplyInfo.getCreatorPartyOrgName();
        }
    },
    CREATOR_NAME(1,3) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return workApplyInfo.getCreatorName();
        }
    },
    CREATOR_MOBILE(1,5) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return workApplyInfo.getCreatorMobile();
        }
    },
    /**
     * 用印时间.
     */
    CHAPTER_DATE(1,7) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return DateFormatUtils.format(workApplyInfo.getChapterDate(), DateHelper.DEFAULT_DATE_FORMAT);
        }
    },
    /**
     * 用印原因（用印资料）.
     */
    CHAPTER_DOC(3,2) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            if (ChapterDocTypeEnum.OTHER == workApplyInfo.getChapterDoc()) {
                return workApplyInfo.getChapterDocOther();
            } else {
                return workApplyInfo.getChapterDoc().getName();
            }
        }
    },
    DOC_NUM(5,2) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return String.valueOf(workApplyInfo.getDocNum());
        }
    },
    SEND_UNIT(6,2) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return workApplyInfo.getSendUnit();
        }
    },
    /**
     * 内容概要.
     */
    CONTENT_SUMMARY(7,2) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return workApplyInfo.getContentSummary();
        }
    },
    /**
     * 支部书记审批.
     */
    ZBSJ(10,1) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return getApproveInfo(workApplyInfo, WorkStatusEnum.ZBSJ.getValue());
        }
    },
    /**
     * 直属党委经办人审批.
     */
    ZSDWJBR(12,1) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return getApproveInfo(workApplyInfo, WorkStatusEnum.ZSDWJBR.getValue());
        }
    },
    /**
     * 直属党委相关负责人审批.
     */
    ZSDWFZR(14,1) {
        public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
            return getApproveInfo(workApplyInfo, WorkStatusEnum.ZSDWFZR.getValue());
        }
    };

    ChapterApproveFormExcelEnum(final int rowIndex, final int cellIndex) {
        this.rowIndex = rowIndex;
        this.cellIndex = cellIndex;
    }

    /**
     * 行列.
     */
    private final int rowIndex;

    /**
     * 单元格列
     */
    private final int cellIndex;

    /**
     * 获取审批意见.
     * @param workApplyInfo 工单信息
     * @param nodeId 审批节点
     * @return 用于excel显示的审批信息
     */
    private static String getApproveInfo(final WorkApplyInfo workApplyInfo, final String nodeId) {
        final Optional<WorkApproveOpinion> lastApproveNode = workApplyInfo.getWorkApproveOpinions().stream().
                filter(item -> nodeId.equals(item.getNodeId())).max(Comparator.comparing(WorkApproveOpinion::getApproveTime));
        if (lastApproveNode.isPresent()) {
            final WorkApproveOpinion workApproveOpinion = lastApproveNode.get();
            return  workApproveOpinion.getOpinion() + "\n          " + workApproveOpinion.getApproveName();
        }
        return StringUtils.EMPTY;
    }
    /**
     * 设置单元格
     * @param sheet excel sheet页
     * @param workApplyInfo 工单
     */
    public void setCellValue(final Sheet sheet, final WorkApplyInfo workApplyInfo) {
        final String value = getValueFormWorkApplyInfo(workApplyInfo);
        sheet.getRow(rowIndex).getCell(cellIndex).setCellValue(value);
    }

    public String getValueFormWorkApplyInfo(final WorkApplyInfo workApplyInfo) {
        return null;
    }
}
